//
//  Created by 刘超 on 15/4/26.
//  Copyright (c) 2015年 Leo. All rights reserved.
//
//  Email:  leoios@sina.com
//  GitHub: http://github.com/LeoiOS
//  如有问题或建议请给我发Email, 或在该项目的GitHub主页lssues我, 谢谢:)
//

#import "LCActionSheet.h"
//
#import "WCMaskView.h"
#import "WCButton.h"
//
#import "Macros.h"

// 按钮高度
#define BUTTON_H 49.0f

@interface LCActionSheet () {
    
    /** 所有按钮 */
    NSArray *_buttonTitles;
}

@property (weak, nonatomic) UIView *bottomView;

@end

@implementation LCActionSheet

+ (instancetype)sheetWithTitle:(NSString *)title
                  buttonTitles:(NSArray *)titles
                redButtonIndex:(NSInteger)buttonIndex
                      delegate:(id<LCActionSheetDelegate>)delegate
{
    return [[self alloc] initWithTitle:title buttonTitles:titles redButtonIndex:buttonIndex delegate:delegate];
}

- (instancetype)initWithTitle:(NSString *)title
                 buttonTitles:(NSArray *)titles
               redButtonIndex:(NSInteger)buttonIndex
                     delegate:(id<LCActionSheetDelegate>)delegate
{
    
    if (self = [super init]) {
        _delegate = delegate;
        
        // Mask View
        WCMaskView *maskView = [[WCMaskView alloc] init];
        [self addSubview:maskView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
        [maskView addGestureRecognizer:tap];
        
        // 所有按钮的底部view
        UIView *bottomView = [[UIView alloc] init];
        [bottomView setBackgroundColor:HexRGB(0xd6d6de)];
        [self addSubview:bottomView];
        _bottomView = bottomView;
        
        if (title) {
            // 标题
            UILabel *label = [[UILabel alloc] init];
            [label setText:title];
            [label setTextColor:HexRGB(0x6f6f6f)];
            [label setTextAlignment:NSTextAlignmentCenter];
            [label setFont:[UIFont systemFontOfSize:13.0f]];
            [label setBackgroundColor:[UIColor whiteColor]];
            [label setFrame:CGRectMake(0, 0, kScreenWidth, BUTTON_H)];
            [bottomView addSubview:label];
        }
        
        if (titles.count) {
            
            _buttonTitles = titles;
            
            for (int i = 0; i < titles.count; i++) {
                CGFloat btnY = BUTTON_H * (i + (title ? 1 : 0));
                
                WCButton *wcBtn = [WCButton txtButton:titles[i] frame:CGRectMake(0, btnY, kScreenWidth, BUTTON_H)];
                [wcBtn setTag:i];
                wcBtn.bg = [UIColor whiteColor];
                wcBtn.downBg = HexRGB(0xe6e6e8);
                wcBtn.titleLabel.font = [UIFont systemFontOfSize:16];
                
                UIColor *titleColor = nil;
                if (i == buttonIndex) {
                    titleColor = HexRGB(0xff0a0a);
                } else {
                    titleColor = [UIColor blackColor] ;
                }
                [wcBtn setTitleColor:titleColor];
                
                [wcBtn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
                
                [bottomView addSubview:wcBtn];
            }
            
            for (int i = 0; i < titles.count; i++) {
                CGFloat lineY = (i + (title ? 1 : 0)) * BUTTON_H;
                
                UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(0, lineY, kScreenWidth, kOnePX)];
                borderView.backgroundColor = kBorderColor;
                [bottomView addSubview:borderView];
            }
        }
        
        // cancel button
        CGFloat btnY = BUTTON_H * (titles.count + (title ? 1 : 0)) + 5.0f;
        WCButton *cancelBtn = [WCButton txtButton:@"取消" frame:CGRectMake(0, btnY, kScreenWidth, BUTTON_H)];
        [cancelBtn setTag:titles.count];
        cancelBtn.bg = [UIColor whiteColor];
        cancelBtn.downBg = HexRGB(0xe6e6e8);
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [cancelBtn setTitleColor:[UIColor blackColor]];
        [cancelBtn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:cancelBtn];
        
        CGFloat bottomH = (title ? BUTTON_H : 0) + BUTTON_H * titles.count + BUTTON_H + 5.0f;
        [bottomView setFrame:CGRectMake(0, kScreenHeight, kScreenWidth, bottomH)];
        
        [self setFrame:kScreenFrame];
    }
    
    return self;
}


- (void)didClickBtn:(UIButton *)btn
{
    [self dismiss:nil];
    [_delegate actionSheet:self didClickedButtonAtIndex:btn.tag];
}

- (void)dismiss:(UITapGestureRecognizer *)tap
{
    
    [UIView animateWithDuration:.25 delay:0 options:7<<16 animations:^{
        CGRect frame = _bottomView.frame;
        frame.origin.y += frame.size.height;
        [_bottomView setFrame:frame];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)showInView:(UIView *)view
{
    [view addSubview:self];
    [UIView animateWithDuration:.25 delay:0 options:7<<16 animations:^{
        CGRect frame = _bottomView.frame;
        frame.origin.y -= frame.size.height;
        [_bottomView setFrame:frame];
    } completion:nil];
}

@end
